package com.bigdata.spark.flume

import java.sql.Connection

import com.mchange.v2.c3p0.ComboPooledDataSource

/**
 * @author Gerry chan
 * @version 1.0
 * sparksql分析nginx日志：https://www.imooc.com/video/19728
 */
object MysqlPoolManager {
  //以单例的形式生成c3p0连接池
  var mysqlManager: MysqlPool = _
  synchronized{
    if (mysqlManager == null) {
      mysqlManager = new MysqlPool
    }
    mysqlManager
  }

  class MysqlPool extends Serializable {
    //连接池配置
    private val dataSource:ComboPooledDataSource =
      new ComboPooledDataSource(true)
    dataSource.setUser("root");
    dataSource.setPassword("123456")
    dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8")
    dataSource.setDriverClass("com.mysql.jdbc.Driver")

    //取出连接
    def getConnection:Connection = {
      dataSource.getConnection()
    }
    //关闭连接
    def getClose:Unit = {
      dataSource.close()
    }
  }


}
